import json
from flask import  Flask, render_template, send_file, send_from_directory,json, jsonify,make_response
from runCell import run
from flask_cors import CORS
import os

app=Flask(__name__)
CORS(app, supports_credentials=True)

@app.route('/run/<file_name>', methods=['GET'])
def index(file_name):
    result =  run(file_name)
    
    return json.dumps({
        'msg': result,
        'code': 200
    })
    
@app.route('/get_file/<file_name>', methods=['GET'])
def get_file(file_name):
    
    print("拿到参数：", file_name)
    directory = 'D:/works/notebook/PPP'
    os.system('jupyter nbconvert --to script D:/works/notebook/PPP/'+file_name+'.ipynb')
    
    try:
        response = make_response(
            send_from_directory(directory, file_name + '.py', as_attachment=True))
        return response
    except Exception as e:
        return jsonify({"code": "异常", "message": "{}".format(e)})
    
if __name__=="__main__":
    app.run(port=8085,host="0.0.0.0",debug=True)